<template>
  <div
    class="inline-container lm-input"
    :class="{ 'lm-input_suffix': showSuffix, lm_input_prefix: showPrefix }"
  >
    <template v-if="type === 'textarea'">
      <textarea
        :name="name"
        v-bind="$attrs"
        :value="value"
        :disabled="disabled"
        :readonly="readonly"
        class="lm-textarea"
        :class="[{ 'is-disabled ': disabled }]"
        @input="handleInput"
        @focus="handleFocus"
        @blur="handleBlur"
        @change="handleChange"
        :style="textareaStyle"
      ></textarea>
    </template>
    <template v-else>
      <div class="inline-container" :class="inlneClass">
        <!-- 前缀 -->
        <div v-if="$slots.prepend" class="lm-input-prepend">
          <!-- <span class="lm_input_prefix"> -->
          <slot name="prepend"></slot>
          <!-- </span> -->
        </div>
        <div class="lm-input-outer">
          <input
            :type="showType"
            :name="name"
            v-bind="$attrs"
            :value="value"
            :disabled="disabled"
            :readonly="readonly"
            class="lm-input_inner"
            :class="inputClass"
            @input="handleInput"
            @focus="handleFocus"
            @blur="handleBlur"
            @change="handleChange"
          />
          <span class="lm-input_suffix" v-if="showSuffix">
            <!-- 清空按钮 -->
            <i
              class="lm-icon-close"
              v-if="clearable && value"
              @click="clear"
            ></i>
            <!-- 密码展示 -->
            <i
              class="lm-icon-eye_protection"
              :class="{ 'lm-icon-view-active': passwordVisible }"
              v-if="showPassword && type === 'password'"
              @click="handlePassword"
            ></i>
          </span>
        </div>
        <!-- 后缀 -->
        <div class="lm-input-shuffend" v-if="$slots.shuffend">
          <!-- <span class="lm-input_suffix"> -->
          <slot name="shuffend"></slot>
          <!-- </span> -->
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import { oneOf } from "../../utils/index";
export default {
  name: "LmInput",
  data() {
    return {
      passwordVisible: false, // 用于控制是否都显示密码框
    };
  },
  props: {
    type: {
      type: String,
      default: "text",
    },
    name: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    value: {
      type: [String, Number],
      default: "",
    },
    clearable: {
      type: Boolean,
      default: false,
    },
    showPassword: {
      type: Boolean,
      default: false,
    },
    readonly: {
      type: Boolean,
      default: false,
    },
    row: {
      // textarea的高度
      type: Number,
      default: 10,
    },
    autosize: {
      //textarea的限制高度
      type: Array,
      validator: (val) => {
        if (val.length === 2) {
          return val;
        } else {
          val.length < 2
            ? console.error("autosize长度小于2")
            : console.error("autosize长度大于2");
        }
      },
      default: () => [2, 100],
    },
    size: {
      type: String,
      validator: (val) => {
        return oneOf(val, ["medium", "small"]);
      },
      default: "medium",
    },
    center: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    // 展示前缀
    showPrefix() {
      return this.$slots.prepend;
    },
    // 展示的后缀
    showSuffix() {
      return this.clearable || this.showPassword;
    },
    // 判断input应该展示的类型
    // 父组件是否是password,是-->判断是否展示密码-->是，类型就是"text",否,类型就是"password"
    // 否 --->直接使用传入进来的type
    showType() {
      return this.showPassword
        ? this.passwordVisible
          ? "text"
          : "password"
        : this.type;
    },
    // textarea的样式
    textareaStyle() {
      return {
        height: `${this.row * 20}px`,
        minHeight: `${this.autosize[0] * 20}px`,
        maxHeight: `${this.autosize[1] * 20}px`,
      };
    },
    // input的样式
    inputClass() {
      console.log(this.center, "center");
      return {
        [`lm-input-${this.size}`]: this.size,
        ["is-disabled"]: this.disabled,
        ["input-align"]: this.center,
      };
    },
    // 含有前缀和后缀的样式
    inlneClass() {
      return {
        "has-prepend": this.$slots.prepend,
        "has-shuffend": this.$slots.shuffend,
      };
    },
  },
  methods: {
    handleInput(e) {
      this.$emit("input", e.target.value);
      // 给input的父级元素传递事件
      this.$parent.$emit("validate");
    },
    // 清空
    clear() {
      this.$emit("input", "");
    },
    handlePassword() {
      this.passwordVisible = !this.passwordVisible;
    },
    handleFocus(e) {
      console.log("focus");
      this.$emit("focus", e);
    },
    handleBlur(e) {
      console.log("blur");
      this.$emit("blur", e);
    },
    handleChange(e) {
      console.log("change");
      this.$emit("change", e);
    },
  },
};
</script>

<style lang="scss" scope>
.lm-input {
  width: 100%;
  position: relative;
  font-size: 14px;
  display: flex;
  align-items: center;

  .lm-input_inner {
    width: 100%;
    -webkit-appearance: none;
    background-color: #fff;
    background-image: none;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    outline: none;
    padding: 0 15px;
    // transition: border-color 0.1s cubic-bezier(0.645, 045, 0.355, 1);
    width: 100%;

    &:focus {
      outline: none;
      border-color: #409eff;
    }
    // input禁用样式
    &.is-disabled {
      background-color: #f5f7fa;
      border-color: #e4e7ed;
      color: #c0c4cc;
      cursor: not-allowed;
    }
  }
}
// 前缀样式
.lm_input_prefix {
  .lm-input_inner {
    // padding-left: 30px;
  }
  .lm_input_prefix {
    position: absolute;
    left: 10px;
    height: 100%;
    top: 0;
    line-height: 40px;
    text-align: center;
    color: #c0c4cc;
    transition: all 0.3s;
    z-index: 900;

    i {
      color: #c0c4cc;
      font-size: 20px;
      cursor: pointer;
      transition: color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    }
    .lm-icon-view-active {
      color: blue;
    }
  }
}
// 后面加suffix的意思是后面如果有后缀的话，触发该样式
.lm-input_suffix {
  .lm-input_inner {
    padding-right: 30px;
  }
  .lm-input_suffix {
    position: absolute;
    right: 10px;
    height: 100%;
    top: 0;
    line-height: 40px;
    text-align: center;
    color: #c0c4cc;
    transition: all 0.3s;
    z-index: 900;
    i {
      color: #c0c4cc;
      font-size: 20px;
      cursor: pointer;
      transition: color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
    }
    .lm-icon-view-active {
      color: blue;
    }
  }
}
// textarea
.lm-textarea {
  background-color: #fff;
  background-image: none;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  outline: none;
  padding: 15px;
  // transition: border-color 0.2s cubic-bezier(0.645, 045, 0.355, 1);
  width: 100%;

  &:focus {
    outline: none;
    border-color: #409eff;
  }
  // input禁用样式
  &.is-disabled {
    background-color: #f5f7fa;
    border-color: #e4e7ed;
    color: #c0c4cc;
    cursor: not-allowed;
  }
}
// input的大小
.lm-input-medium {
  height: 40px;
  line-height: 40px;
}

.lm-input-small {
  height: 35px;
  line-height: 35px;
}
// 文字水平居中
.input-align {
  text-align: center;
}
// 含有前后缀的样式
.lm-input-outer {
  width: 100%;
  position: relative;
}
.inline-container {
  width: 100%;
  display: inline-flex;
}
.lm-input-prepend,
.lm-input-shuffend {
  background-color: #f5f7fa;
  color: #909399;
  display: flex;
  align-items: center;
  border: 1px solid #dcdfe6;
  border-radius: 0px;
  padding: 0 10px;
  width: 1px;
  white-space: nowrap;
  width: max-content;
}
.has-prepend .lm-input_inner {
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
  border-left: none;
}

.has-shuffend .lm-input_inner {
  border-top-right-radius: 0px;
  border-bottom-right-radius: 0px;
  border-right: none;
}

.has-prepend .lm-input-prepend {
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
}

.has-prepend .lm-input-shuffend {
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}

.has-shuffend .lm-input-shuffend {
  border-top-right-radius: 4px;
  border-bottom-right-radius: 4px;
}
</style>